%{
    #include <stdlib.h>
    #include "gram.tab.h"
    #include "Hashmap.h"

    Hashmap *gMap=NULL; //globalMap
    Hashmap *map=NULL;
    int isStarted = 0;
%}

beginDoc    \\begin\{document\}
endDoc      \\end\{document\}

start       \\begin\{algo\}\{.*\}
end         \\end\{algo\}
input       \\Input
output      \\Output
in          \\in
constant    \\Constant
global      \\Global
local       \\Local
blank       \\BlankLine
affectation \\leftarrow
mult        \\times
emptyset    \\emptyset
delimiter   \\;
repeat      \\Repeat

type        \\(Integer|Boolean|Real|Complex)

tif         \\If
eif         \\eIf
lwhile      \\While
tfor        \\For

mot         [a-zA-Z]+[a-zA-Z0-9]*
boolean     \\faux|\\vrai|\\true|\\false

kwto        \\KwTo
kwin        \\KwIn.*\n
kwout       \\KwOut.*\n
mbox        \\mbox
print       \\print
scan        \\scan

logicop     \\(vee|wedge|neg|neq|eq|leq|geq|lt|gt)

%%

{beginDoc}      { 
                    gMap = newScopeTable(NULL,0);
                    map = gMap;
                    return beginDoc;
                }
{endDoc}        { return endDoc; }

[0-9]*          { if(isStarted){ printf("const "); yylval.Value.integer = atoi(yytext); return entier;} }
{mot}           { if(isStarted){ printf("mot "); 
                        char *buf = strndup(yytext, strlen(yytext)); 
                        yylval.Value.string = buf; return ident; 
                    }
                }
[ ]*            {}
\n              { if(isStarted) printf("\n");}
.               { if(isStarted){ printf("%c ", yytext[0]); return yytext[0]; }}

{start}         { if(isStarted) yyterminate(); isStarted=1;
                    char *buf = strndup(&yytext[13], strlen(yytext)-14); 
                    yylval.v = addInTable(map, buf);
                    if(yylval.v == NULL){
                        printf("La fonction %s est déjà définie\n", buf);
                        free(buf);
                        yyterminate();
                    }
                    free(buf);
                    yylval.v->Value.isFct = 1;
                    map = newScopeTable(map,0);
                    printf("start\n"); return start; 
                }
{end}           { if(!isStarted){ yyterminate(); } isStarted=0; 
                    printf("end\n"); 
                    Hashmap *tmp=map; 
                    map=map->parentMap; 
                    //freeTable(tmp); 
                    return end;
                }
{input}         { if(isStarted){ printf("input "); return tinput; }}
{output}        { if(isStarted){ printf("output "); return toutput; }}
{in}            { if(isStarted){ printf("in "); return in; }}
{boolean}       { if(isStarted){ printf("bool "); 
                        yylval.Value.integer = -1;
                        if(yytext[1]=='t' || yytext[1]=='v')
                            yylval.Value.integer = 1;
                        else if(yytext[1]=='f')
                            yylval.Value.integer = 0;
                        return boolean; 
                    }
                }
{constant}      { if(isStarted){ printf("const "); return constant; }}
{global}        { if(isStarted){ printf("global "); return global; }}
{local}         { if(isStarted){ printf("local "); return local; }}  
{emptyset}      { if(isStarted){ printf("empty "); return emptyset;} }
{lwhile}        { if(isStarted){ printf("while "); return twhile; }}
{affectation}   { if(isStarted){ printf("affect "); return affectation; }}
{mult}          { if(isStarted){ printf("mult ");  return mult; }}
{type}          { if(isStarted){ 
                  printf("type ");
                        if(strcmp(&yytext[1], "Integer")==0)
                            yylval.Value.integer=ENTIER;
                        else if(strcmp(&yytext[1], "Real")==0)
                            yylval.Value.integer=FLOAT;
                        else if(strcmp(&yytext[1], "Boolean")==0)
                            yylval.Value.integer=BOOL;
                        else if(strcmp(&yytext[1], "Complex")==0)
                            yylval.Value.integer=COMPLEX;
                        else{
                            printf("Unknow type, abooooort !\n");
                            yylval.Value.integer=-1;
                        }
 
                        return type; 
                    }
                }
{delimiter}     { if(isStarted){ printf("delim "); return delimiter; }}
{logicop}     { if(isStarted){ printf("boolop "); 
                        if(strcmp(&yytext[1], "vee")==0)
                            yylval.Value.integer=VEE;
                        else if(strcmp(&yytext[1], "wedge")==0)
                            yylval.Value.integer=WEDGE;
                        else if(strcmp(&yytext[1], "neg")==0)
                            yylval.Value.integer=NEG;
                        else if(strcmp(&yytext[1], "neq")==0)
                            yylval.Value.integer=NEQ;
                        else if(strcmp(&yytext[1], "eq")==0)
                            yylval.Value.integer=EQ;
                        else if(strcmp(&yytext[1], "leq")==0)
                            yylval.Value.integer=LEQ;
                        else if(strcmp(&yytext[1], "geq")==0)
                            yylval.Value.integer=GEQ;
                        else if(strcmp(&yytext[1], "lt")==0)
                            yylval.Value.integer=LT;
                        else if(strcmp(&yytext[1], "gt")==0)
                            yylval.Value.integer=GT;
                        return logicop; 
                    }
                }
{kwto}          { if(isStarted){ printf("kwto "); return kwto; }}
{kwin}          { if(isStarted){ printf("kwin ");}}
{kwout}         { if(isStarted){ printf("kwout ");}}
{tfor}          { if(isStarted){ printf("tfor "); return tfor; }}
{tif}           { if(isStarted){ printf("if "); return tif; }}
{eif}           { if(isStarted){ printf("eif "); return eif; }}
{mbox}          { if(isStarted){ printf("mbox "); return mbox; }}
{print}         { if(isStarted){ printf("print "); return print; }}
{scan}          { if(isStarted){ printf("scan "); return scan; }}

{blank}         {}
<<EOF>>         { yyterminate(); }

%%
